package com.qqqq.web.user;

import com.alibaba.fastjson2.JSONObject;
import com.qqqq.code.AjaxResult;
import com.qqqq.code.BaseHttpServlet;
import com.qqqq.dao.impl.UserDaoImpl;
import com.qqqq.domain.User;
import com.qqqq.service.IUserService;
import com.qqqq.service.impl.UserServiceImpl;
import com.qqqq.utils.RequestUtils;
import com.qqqq.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user_login")
public class LoginServlet extends BaseHttpServlet {
    private IUserService userService = new UserServiceImpl(new UserDaoImpl());


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        JSONObject json = RequestUtils.getReqJsonBody(req, JSONObject.class);
//        try {
//            User user =userService.login(json.getString("username"), json.getString("password"));
//            ResponseUtils.responseJson(resp,AjaxResult.success(user));
//        } catch (Exception e) {
//            ResponseUtils.responseJson(resp,AjaxResult.error(500, e.getMessage()));
//        }

        JSONObject json = RequestUtils.getReqJsonBody(req, JSONObject.class);
        executeBusiness(resp, response -> {
            User user = userService.login(json.getString("username"), json.getString("password"));
            ResponseUtils.responseJson(resp, AjaxResult.success(user));
        });
    }
}